<template>
  <p>不可重复执行！</p>
  <el-button
    type="primary"
    :loading="loadData.field"
    @click="submitRepairField()"
    :disabled="props.isDisabled"
  >
    更新address字段数据
  </el-button>
  <el-button
    type="primary"
    :loading="loadData.field"
    @click="submitRepairAwardUser()"
    :disabled="props.isDisabled"
  >
    根据奖励，更新订单表的上下级
  </el-button>
  <el-button
    type="primary"
    :loading="loadData.field"
    @click="submitRepairCancelAwardUser()"
    :disabled="props.isDisabled"
  >
    取消部分订单收益
  </el-button>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import {
  getRepairList,
  getRepairDetail,
  updateRepair,
  actionRepairModule
} from '@/apis/repair';

const dbname = 'order';

// props
const props = defineProps({
  isDisabled: {
    type: Boolean,
    default: false
  }
});

const loadData = reactive({
  field: false
});

/**
 * 更新字段
 */
const submitRepairField = async (page: number = 1) => {
  loadData.field = true;

  // 每次取20条数据
  const maxLimit = 20;
  const resList = await getRepairList(dbname, { page, limit: maxLimit });

  if (!resList.data.data || !resList.data.data.length) {
    console.warn('执行结束');
    loadData.field = false;
    return;
  }

  // 循环所有数据
  for (let i = 0; i < resList.data.data.length; i++) {
    const item = resList.data.data[i];

    const res = await getRepairDetail(dbname, item.id!);

    const dataOpt: any = {};

    // 检查收货地址是否已更新
    if (res.data.takeInfo) continue;

    // 判断收货地址isNull
    if (!res.data.otnAddress) continue;

    console.group('更新table字段');
    console.log(
      `[Limit] - ${maxLimit} -- [Page] - ${page} -- [Current] - ${i + 1}`
    );
    console.log(`[ID] - ${res.data.id}`);
    console.log(`[ownId] - ${res.data.ownId}`);
    console.log(`[orderNo] - ${res.data.orderNo}`);

    console.log(`--------更新字段--------`);

    const takeInfo = transformAddress(res.data.otnAddress, res.data);
    dataOpt.takeInfo = JSON.stringify(takeInfo);

    console.log(`[takeInfo - Raw] - ${res.data.otnAddress}`);
    console.log(`[takeInfo - New] - ${dataOpt.takeInfo}`);
    console.groupEnd();

    try {
      await updateRepair(dbname, res.data.id!, dataOpt);
    } catch (error) {
      console.error('submitRepairField', error);
      loadData.field = false;
      return;
    }
  }

  setTimeout(() => {
    submitRepairField(page + 1);
  }, 2 * 1000);
};

// 处理地址
const transformAddress = (address: string, rowData: any) => {
  const regex = /"([^"]*)"/g;

  // 输出: ['id', '1', 'openid', 'xxxxx', 'realname', 'xxx']
  let params: string[] = address.match(regex) || [];
  params = params?.map((str: string) => str.replace(/"/g, ''));

  // 序列化
  const oldData: any = {};
  for (let i = 0; i < params.length; i += 2) {
    oldData[params[i]] = params[i + 1];
  }

  const dataOpt = {
    id: oldData.id,
    userOwnId: rowData.userOwnId,
    contacts: oldData.realname,
    mobile: oldData.mobile,
    province: oldData.province,
    city: oldData.city,
    area: oldData.area,
    street: oldData.address,
    code: oldData.zipcode,
    isDefault: oldData.isdefault
  };

  return dataOpt;
};

/**
 * 根据奖励，更新订单表的上下级
 */
const submitRepairAwardUser = async (page: number = 1) => {
  loadData.field = true;

  // 每次取1条数据
  const maxLimit = 1;
  const resList = await getRepairList(dbname, { page, limit: maxLimit });

  if (!resList.data.data || !resList.data.data.length) {
    console.warn('执行结束');
    loadData.field = false;
    return;
  }

  // 循环所有数据
  for (let i = 0; i < resList.data.data.length; i++) {
    const item = resList.data.data[i];

    console.group('更新order字段');
    console.log(
      `[Limit] - ${maxLimit} -- [Page] - ${page} -- [Current] - ${i + 1}`
    );
    console.log(`[ID] - ${item.id}`);
    console.groupEnd();

    try {
      await actionRepairModule('createAwardWithUser', { id: item.id });
    } catch (error) {
      console.error('submitRepairAwardUser', error);
      loadData.field = false;
      return;
    }
  }

  setTimeout(() => {
    submitRepairAwardUser(page + 1);
  }, 1 * 1000);
};

/**
 * 取消部分订单收益
 */
const submitRepairCancelAwardUser = async () => {
  loadData.field = true;

  try {
    await actionRepairModule('cancelOrderByAwardUser', null);
  } catch (error) {
    console.error('submitRepairCancelAwardUser', error);
    loadData.field = false;
    return;
  }

  console.warn('执行结束');
  loadData.field = false;
};
</script>

<style lang="scss" scoped></style>
